﻿namespace LevelEditors
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.CompilerServices;
    using UnityEngine;
    using UnityEngine.EventSystems;
    using UnityEngine.UI;

    [RequireComponent(typeof(Toggle))]
    public class UIToggle : MonoBehaviour, IBeginDragHandler, IEventSystemHandler, IEndDragHandler, IDragHandler
    {
        [CompilerGenerated]
        private static Action <>f__am$cache11;
        [CompilerGenerated]
        private static Action<string> <>f__am$cache12;
        [CompilerGenerated]
        private static Action <>f__am$cache13;
        public bool allowRename;
        public bool allowReorder;
        public int index;
        private float initialHeight;
        private float lastClickTime;
        private bool lastValue;
        [SerializeField]
        private Text m_labelText;
        [SerializeField]
        private LayoutElement m_layoutElement;
        [SerializeField]
        private InputField m_renameField;
        private Toggle m_toggle;
        private bool manuallyOn;
        public Action<string> onRename;
        public Action onReorder;
        public Action onSelected;
        private List<RaycastResult> results;

        public UIToggle()
        {
            if (<>f__am$cache11 == null)
            {
                <>f__am$cache11 = new Action(UIToggle.<onSelected>m__3B);
            }
            this.onSelected = <>f__am$cache11;
            if (<>f__am$cache12 == null)
            {
                <>f__am$cache12 = new Action<string>(UIToggle.<onRename>m__3C);
            }
            this.onRename = <>f__am$cache12;
            if (<>f__am$cache13 == null)
            {
                <>f__am$cache13 = new Action(UIToggle.<onReorder>m__3D);
            }
            this.onReorder = <>f__am$cache13;
            this.results = new List<RaycastResult>();
        }

        [CompilerGenerated]
        private static void <onRename>m__3C(string s)
        {
        }

        [CompilerGenerated]
        private static void <onReorder>m__3D()
        {
        }

        [CompilerGenerated]
        private static void <onSelected>m__3B()
        {
        }

        private void Awake()
        {
            this.initialHeight = this.m_layoutElement.minHeight;
            this.m_renameField.onEndEdit.AddListener(delegate (string x) {
                this.m_labelText.text = x;
                this.m_renameField.gameObject.SetActive(false);
                this.onRename(x);
            });
            this.toggle.onValueChanged.AddListener(delegate (bool value) {
                if ((value && (this.onSelected != null)) && (!this.manuallyOn && !this.lastValue))
                {
                    this.onSelected();
                }
                this.manuallyOn = false;
                if ((this.lastValue && value) && (this.allowRename && ((Time.time - this.lastClickTime) < 0.5f)))
                {
                    this.m_renameField.gameObject.SetActive(true);
                    this.m_renameField.text = this.m_labelText.text;
                }
                else
                {
                    this.m_renameField.gameObject.SetActive(false);
                }
                this.lastValue = value;
                this.lastClickTime = Time.time;
            });
        }

        private UIToggle GetTarget(PointerEventData eventData)
        {
            EventSystem.current.RaycastAll(eventData, this.results);
            foreach (RaycastResult result in this.results)
            {
                if (result.gameObject != base.gameObject)
                {
                    UIToggle componentInParent = result.gameObject.GetComponentInParent<UIToggle>();
                    if (componentInParent != null)
                    {
                        return componentInParent;
                    }
                }
            }
            return null;
        }

        public void OnBeginDrag(PointerEventData eventData)
        {
            if (this.allowReorder)
            {
                this.toggle.interactable = false;
            }
        }

        public void OnDrag(PointerEventData eventData)
        {
            if (this.allowReorder)
            {
                Vector2 position = eventData.position;
                position.x = base.transform.position.x;
                base.transform.position = (Vector3) position;
                UIToggle target = this.GetTarget(eventData);
                if (target != null)
                {
                    int num = (target.transform.position.y <= eventData.position.y) ? -1 : 0;
                    base.transform.SetSiblingIndex(target.transform.GetSiblingIndex() + num);
                }
            }
        }

        private void OnEnable()
        {
        }

        public void OnEndDrag(PointerEventData eventData)
        {
            if (this.allowReorder)
            {
                this.onReorder();
                this.toggle.interactable = true;
            }
        }

        public FontStyle fontStyle
        {
            get
            {
                return this.m_labelText.fontStyle;
            }
            set
            {
                this.m_labelText.fontStyle = value;
            }
        }

        public ToggleGroup group
        {
            get
            {
                return this.toggle.group;
            }
            set
            {
                this.toggle.group = value;
            }
        }

        public string guid { get; set; }

        public bool isHided
        {
            get
            {
                return (this.m_layoutElement.minHeight == 0f);
            }
            set
            {
                this.m_layoutElement.minHeight = !value ? this.initialHeight : 0f;
            }
        }

        public bool isOn
        {
            get
            {
                return this.toggle.isOn;
            }
            set
            {
                this.toggle.isOn = value;
                if (value)
                {
                    this.manuallyOn = true;
                }
            }
        }

        public string label
        {
            get
            {
                return this.m_labelText.text;
            }
            set
            {
                this.m_labelText.text = value;
            }
        }

        public object target { get; set; }

        private Toggle toggle
        {
            get
            {
                return ((this.m_toggle == null) ? (this.m_toggle = base.GetComponent<Toggle>()) : this.m_toggle);
            }
        }

        public RectTransform tsm
        {
            get
            {
                return base.GetComponent<RectTransform>();
            }
        }
    }
}

